package logic

import (
	"context"
	"errors"

	"go-zero_try/mall/order/rpc/pb/internal/svc"
	"go-zero_try/mall/order/rpc/pb/pb"
	"go-zero_try/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *pb.LoginReq) (*pb.LoginReps, error) {
	u1, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UID)
	msg := ""
	u2 := model.Users{
		Uid:      "0",
		Username: "unknown",
		Age:      "-1",
		Password: "",
	}
	if err != nil {
		msg = "用户不存在"
		return &pb.LoginReps{
			Msg: msg,
			UID: u2.Uid,
		}, errors.New("")
	} else {
		if in.Password != u1.Password {
			msg = "密码错误"
		} else {
			msg = "登陆成功"
			u2.Uid = in.UID
			u2.Username = u1.Username
			u2.Age = u1.Age
			u2.Password = u1.Password
		}
	}
	return &pb.LoginReps{
		Msg:  msg,
		UID:  u2.Uid,
		Name: u2.Username,
		Age:  u2.Age,
		Pwd:  u2.Password,
	}, nil
}
